import {
  INIT_BUYCART,
  ADD_CART,
  GET_USERINFO,
  RECORD_USERINFO,
  ADD_ANIMATION,
  SHOW_CART,
  REDUCE_CART,
  EDIT_CART,
  DEL_CAR,
  CHANGE_COST,
  ALL_CAR,
  POINT_CAR
} from './mutation-types'
import { setStore, getStore } from '../utils/storage'
// import checkout from "../page/Checkout/checkout";
import { cartEdit } from '../api/goods'
export default {
  // 网页初始化时从本地缓存获取购物车数据
  [INIT_BUYCART] (state) {
    let initCart = JSON.parse(getStore('buyCart'))
    // console.log(initCart[0].goodsDtoList)
    setStore('cars', initCart)
    if (initCart) {
      let newArr = []
      for (let i = 0; i < initCart[0].goodsDtoList.length; i++) {
        newArr[i] = initCart[0].goodsDtoList[i]
      }
      state.cartList = newArr
      state.cartAllprice = initCart[0].price
    }
  },
  [DEL_CAR] (state, id) {
    var cars = JSON.parse(getStore('cars'))
    // var data = cars[0].goodsDtoList
    var a = []
    // eslint-disable-next-line no-unused-vars
    var sum = 0
    for (var i = 0; i < state.cartList.length; i++) {
      if (state.cartList[i].goods.id !== id) {
        a.push(state.cartList[i])
        sum += state.cartList[i].price
      }
    }
    state.cartList = a
    state.cartAllprice = sum
    cars[0].goodsDtoList = state.cartList
    cars[0].price = sum
    setStore('cars', cars)
    // cartEdit(cars).then(res => {
    //   console.log(1)
    // })
  },
  // 加入购物车
  [ADD_CART] (state, {productId, salePrice, productName, productImg, productNum = 1, stock, details, type}) {
    let cart = state.cartList // 购物车
    // console.log('-----------')
    // console.log(cart)
    let falg = true
    let cars = JSON.parse(getStore('cars'))
    var arr = []
    for (var i = 0; i < cars[0].goodsDtoList.length; i++) {
      arr[arr.length] = cars[0].goodsDtoList[i]
    }
    let goods = {
      goods: {
        checked: 0,
        createTime: 'string',
        details: details,
        id: productId,
        img: productImg,
        isdelete: '0',
        name: productName,
        price: salePrice,
        stock: stock,
        type: type,
        updateTime: 'string'
      },
      nums: productNum,
      price: productNum * salePrice
    }
    if (cart.length) {        // 有内容
      // console.log(cart)
      for (var j = 0; j < cart.length; j++) {
        var item = cart[j]
        console.log(item.goods.id === productId)
        console.log(item.goods.id, productId)
        if (item.goods.id === productId) {
          if (item.nums >= 0) {
            falg = false
            item.nums += productNum
            item.price = item.nums * item.goods.price
            // arr[j].nums += productNum
            // arr[j].price = item.nums * item.goods.price
          }
        }
      }
    }
    // console.log(falg)
    if (!cart.length || falg) {
      // console.log(2222)
      goods.nums = productNum
      cart.push(goods)
      // arr.push(goods)
    }
    var sum = 0
    cart.forEach(e => {
      sum += e.price
    })
    state.cartAllprice = sum
    // console.log(cart)
    // cars[0].price = sum
    // cars[0].goodsDtoList = arr
    state.cartList = cart
    // 存入localStorage
    // setStore('cars', cars)
    // let params = {
    //   GoodShopping: cars[0],
    //   token: getStore('token')
    //   // userId: parseInt(getStore('userId'))
    // }
    //
    // console.log(params)
    // cartEdit(params).then(res => {
    //   console.log('成功')
    // })
  },
  [ALL_CAR] (state) {
    var cars = state.cartList
    var is = 0
    var i
    for (i = 0; i < cars.length; i++) {
      if (cars[i].goods.checked === 0) {
        is = 1
        break
      }
    }
    if (is === 1) {
      for (i = 0; i < cars.length; i++) {
        cars[i].goods.checked = 1
      }
    } else {
      for (i = 0; i < cars.length; i++) {
        cars[i].goods.checked = 0
      }
    }
    state.cartList = cars
  },
  [CHANGE_COST] (state, id, nums = parseInt(getStore('num'))) {
    var items = JSON.parse(getStore('cars'))
    var cars = state.cartList
    var i
    // eslint-disable-next-line no-unused-vars
    var sum = 0
    for (i = 0; i < cars.length; i++) {
      if (id === cars[i].goods.id) {
        cars[i].nums = nums
        cars[i].price = nums * cars[i].goods.price
      }
      sum += cars[i].price
    }
    var arr = []
    for (i = 0; i < items[0].goodsDtoList.length; i++) {
      arr[arr.length] = items[0].goodsDtoList[i]
    }
    state.cartList = cars
    state.cartAllprice = sum
    items[0].goodsDtoList = arr
    items[0].price = sum
    setStore('cars', items)
    let params = {
      GoodShopping: items[0],
      token: getStore('token')
      // userId: parseInt(getStore('userId'))
    }

    // console.log(params)
    cartEdit(params).then(res => {
      console.log('成功')
    })
  },
  [POINT_CAR] (state, id) {
    var cars = state.cartList
    var i
    for (i = 0; i < cars.length; i++) {
      if (id === cars[i].goods.id) {
        cars[i].goods.checked = cars[i].goods.checked === 1 ? 0 : 1
      }
    }
    state.cartList = cars
  },
  // 加入购物车动画
  [ADD_ANIMATION] (state, {moveShow, elLeft, elTop, img, cartPositionT, cartPositionL, receiveInCart}) {
    state.showMoveImg = moveShow
    if (elLeft) {
      state.elLeft = elLeft
      state.elTop = elTop
    }
    state.moveImgUrl = img
    state.receiveInCart = receiveInCart
    if (cartPositionT) {
      state.cartPositionT = cartPositionT
      state.cartPositionL = cartPositionL
    }
  },
  // 是否显示购物车
  [SHOW_CART] (state, {showCart}) {
    // let timer = null
    state.showCart = showCart
    // clearTimeout(timer)
    // if (showCart) {
    //   timer = setTimeout(() => {
    //     state.showCart = false
    //   }, 5000)
    // }
  },
  // 移除商品
  [REDUCE_CART] (state, {productId}) {
    let cart = state.cartList
    cart.forEach((item, i) => {
      if (item.productId === productId) {
        if (item.productNum > 1) {
          item.productNum--
        } else {
          cart.splice(i, 1)
        }
      }
    })
    state.cartList = cart
    // 存入localStorage
    setStore('buyCart', state.cartList)
  },
  // 修改购物车
  [EDIT_CART] (state, {productId, productNum, checked}) {
    let cart = state.cartList
    if (productNum) {
      cart.forEach((item, i) => {
        if (item.goods.id === productId) {
          item.goods.nums = productNum
          item.goods.checked = checked
        }
      })
    } else if (productId) {
      cart.forEach((item, i) => {
        if (item.goods.id === productId) {
          cart.splice(i, 1)
        }
      })
    } else {
      cart.forEach((item) => {
        item.checked = checked ? '1' : '0'
      })
    }
    state.cartList = cart
    // 存入localStorage
    setStore('buyCart', state.cartList)
  },
  // 记录用户信息
  [RECORD_USERINFO] (state, info) {
    state.userInfo = info
    state.login = true
    setStore('userInfo', info)
  },
  // 获取用户信息
  [GET_USERINFO] (state, info) {
    if (state.userInfo && (state.userInfo.username !== info.username)) {
      return
    }
    if (!state.login) {
      return
    }
    if (!info.message) {
      state.userInfo = {...info}
    } else {
      state.userInfo = null
    }
  }
}
